package com.ce.pms.custservice.web;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.custservice.service.IEventManageService;
import com.ce.pms.custservice.vo.CustServiceEventSearchVO;
import com.ce.pms.custservice.vo.CustServiceEventVO;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.system.custservice.model.CustEventKindDO;
import com.ce.pms.system.custservice.service.ICustEventKindService;
import com.ce.pms.system.custservice.service.ICustEventNatureService;
import com.ce.pms.system.custservice.service.IEmergencyDegreeService;
import com.ce.pms.system.custservice.service.IEventRegionService;
import com.ce.pms.system.custservice.service.IFrequentEventService;
import com.ce.pms.system.custservice.service.ISeriousDegreeService;
import com.ce.pms.system.custservice.vo.CustEventKindVO;
import com.ce.pms.system.custservice.vo.CustEventNatureVO;
import com.ce.pms.system.custservice.vo.EmergencyDegreeVO;
import com.ce.pms.system.custservice.vo.EventRegionVO;
import com.ce.pms.system.custservice.vo.FrequentEventVO;
import com.ce.pms.system.custservice.vo.SeriousDegreeVO;
import com.ce.util.BlankUtil;
import com.ce.util.XMLAbout;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

public class CustServEventAction extends PmsBaseAction {

	/**
	 * 保存查询条件key
	 */
	private static final String SEARCH_CONDTION_KEY = "search_condtion_key";

	/**
	 * 保存、或者新建操作KEY。定这个两个变量是因为新增页面同修改页面是同一个页面,需要区别一下。
	 */
	private static final String OPTION_STATUS = "page_option";

	private static final String OPTION_STATUS_ADD = "isAdd";

	private static final String OPTION_STATUS_EDIT = "isEdit";

	private static final String CACHE_KEY_EVENT = "custEventVO";

	private IEventManageService eventManageService; //客户事件Service

	private IEventRegionService eventRegionService; //事件区域

	private ICustEventNatureService custEventNatureService; //事件性质

	private ICustEventKindService custEventKindService; //事件类型

	private IFrequentEventService frequentEventService; //频发事件

	private IEmergencyDegreeService	emergencyDegreeService;//紧急程度

	private ISeriousDegreeService	seriousDegreeService;//严重程度

	private IHouseService houseService = null;  //房屋
	/**
	 * 功能描述: 初始化客服事件主页面<br>
	 * @return
	 * Create author:xuhaidui <br>
	 * Create on:2008-12-11  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initCustomerEventMain(){
		initCustomerEventRegion();//初始化事件区域
		initCustomerEventNature();//初始化事件性质
		return "customerEventMain";
	}

	/**
	 * 功能描述:初始化高级查询页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	public String initGaojiSearch(){
		initCustomerEventRegion();//初始化事件区域
		initCustomerEventNature();//初始化事件性质
		return "gaojiSearch";
	}
	/**
	 * 功能描述: 初始化客服事件列表页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initCustomerEventList(){
		setAttribute("totalRows", Integer.valueOf(0));
		return "customerEventList";
	}

	/**
	 * 功能描述:初始化客服时间添加页面,这里添加和修改是同一页面,所以需要一个标识区别是修改还是添加<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	@SuppressWarnings("static-access")
	public String initCustomerEventAdd(){
		initCustomerEventRegion();//初始化事件区域
		initCustomerEventNature();//初始化事件性质
		initEmergencyDegree();//初始化紧急程度
		initFrequentEvent();//初始化频发事件
		initSeriousDegree();//初始化严重程度
		setAttribute(this.OPTION_STATUS, this.OPTION_STATUS_ADD);
		return "customerEventEdit";
	}

	/**
	 * 功能描述:初始化客服时间修改页面,这里添加和修改是同一页面,所以需要一个标识区别是修改还是添加<br>
	 * @param customerEventId
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	@SuppressWarnings("static-access")
	public String initCustomerEventEdit(@Read(key="customerEventId")Long customerEventId){
		CustServiceEventVO custServiceEventVO = eventManageService.getCustServEventByID(customerEventId);
		setAttribute(this.CACHE_KEY_EVENT, custServiceEventVO);

		initCustomerEventRegion();//初始化事件区域
		initCustomerEventNature();//初始化事件性质
		initEmergencyDegree();//初始化紧急程度
		initFrequentEvent();//初始化频发事件
		initSeriousDegree();//初始化严重程度
		initCustomerEventKind(custServiceEventVO.getEventNatureKeyword());//初始化事件类型

		initOptionStatus(this.OPTION_STATUS_EDIT); //设置操作为修改
		return "customerEventEdit";
	}
	/**
	 * 功能描述:初始化事件作废页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-16  <br>
	 */
	public String initAsideCustEvent(@Read(key="custEventKeyword")
									 @Out(key="custEventKeyword") Long keyword){
		return "sideCustEvent";
	}
	/**
	 * 功能描述:初始化客服事件详细页面<br>
	 * @param keyword
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-23  <br>
	 */
	public String initCustservEventDetail(@Read(key="custEventKeyword")
										@Out(key="custEventKeyword") Long keyword){
		CustServiceEventVO vo =  eventManageService.getCustServEventByID(keyword);
		setAttribute("custServiceEventVO", vo);
		return "detail";
	}
	/**
	 * 功能描述: 初始化客服事件详细页面<br>
	 * @param keyword
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-23  <br>
	 */
	public String initCustservEventDetailMain(@Read(key="custEventKeyword")
			@Out(key="custEventKeyword") Long keyword){
		return "detailMain";
	}
	/**
	 * 功能描述: 查询关联事件<br>
	 * @param custServiceEventSearchVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-23  <br>
	 */
	public String initLinkTheOtherListMain(@Read CustServiceEventSearchVO custServiceEventSearchVO,
											@Read(key = "ec_crd",defaultValue="10")int pageSize,
											 @Read(key = "ec_p", defaultValue="1")	int pageNo){
		custServiceEventSearchVO.setPageNo(pageNo);
		custServiceEventSearchVO.setPageSize(pageSize);
		Page page = eventManageService.queryAssociatedCSEventByPage(custServiceEventSearchVO);

		setAttribute("keyword", custServiceEventSearchVO.getKeyword());

		setAttribute("customerEventList", page.getResult());
		setAttribute("totalRows", Integer.valueOf(String.valueOf(page.getTotalCount())));
		return "linkTheOtherListMain";
	}
	/**
	 * 功能描述:初始化关联事件详细页面<br>
	 * @param custServiceEventSearchVO
	 * @param pageSize
	 * @param pageNo
	 */
	public String initLinkTheOtherListDetail(@Read CustServiceEventSearchVO custServiceEventSearchVO,
											 @Read(key = "ec_crd",defaultValue="10")int pageSize,
											 @Read(key = "ec_p", defaultValue="1")	int pageNo){
		initLinkTheOtherListMain(custServiceEventSearchVO,pageSize,pageNo);
		return "linkTheOtherListDetail";

	}
	/**
	 * 功能描述:初始化事件关联查询主页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-24  <br>
	 */
	public String initCustomerEventSearchMain(@Read(key="keyword") @Out(key="eventId")Long eventId){
		initCustomerEventNature();//初始化事件性质
		return "searchMain";
	}
	/**
	 * 功能描述: 初始化搜索事件列表页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-24  <br>
	 */
	public String initCustomerEventSearchList(){
		this.getRequest().setAttribute("totalRows", Integer.valueOf(String.valueOf(0)));
		return "searchList";
	}
	/**
	 * 功能描述: 事件关联事件查询<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-24  <br>
	 */
	public String queryCustomerEventSearchList(@Read CustServiceEventSearchVO custServiceEventSearchVO,
											   @Read(key = "ec_crd",defaultValue="10")int pageSize,
											   @Read(key = "ec_p", defaultValue="1")	int pageNo){
		custServiceEventSearchVO.setPageNo(pageNo);
		custServiceEventSearchVO.setPageSize(pageSize);
		Page page = eventManageService.queryCustServEventByPage(custServiceEventSearchVO);

		this.getRequest().setAttribute("customerEventList", page.getResult());
		this.getRequest().setAttribute("totalRows", Integer.valueOf(String.valueOf(page.getTotalCount())));
		return "searchList";
	}
	/**
	 * 功能描述: 查询客户事件列表<br>
	 * @param custServiceEventVO 客户事件VO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	public String queryCustomerEventList(@Read(key = "ec_crd",defaultValue="15")int pageSize,
            							 @Read(key = "ec_p", defaultValue="1")	int pageNo){

		CustServiceEventSearchVO custServiceEventSearchVO = getCustomerEventSearchCondtion();
		custServiceEventSearchVO.setPageNo(pageNo);
		custServiceEventSearchVO.setPageSize(pageSize);
		custServiceEventSearchVO.setSortStr(ExtremeUtils.getOrderString(getRequest()));
		Page page = eventManageService.queryCustServEventByPage(custServiceEventSearchVO);

		this.getRequest().setAttribute("customerEventList", page.getResult());
		this.getRequest().setAttribute("totalRows", Integer.valueOf(String.valueOf(page.getTotalCount())));
		return "customerEventList";
	}
	/**
	 * 功能描述: 添加客户事件<br>
	 * @param custServiceEvent
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-16  <br>
	 */
	public String addCSEvent(@Read CustServiceEventVO custServiceEvent,
							 @Read(key="submitStatus")String status){
		custServiceEvent.setCreator(super.getUserId());
		custServiceEvent.setRecorder(super.getUsername());
		CustServiceEventVO vo = eventManageService.addCustEvent(custServiceEvent);
		setAttribute(CACHE_KEY_EVENT, vo); //保存添加的VO到Request

		initCustomerEventRegion();//初始化事件区域
		initCustomerEventNature();//初始化事件性质
		initEmergencyDegree();//初始化紧急程度
		initFrequentEvent();//初始化频发事件
		initSeriousDegree();//初始化严重程度
		initCustomerEventKind(vo.getEventNatureKeyword());//初始化事件类型

		initOptionStatus(OPTION_STATUS_EDIT); //设置操作状态为修改
		initSubmitStatus(true,status);
		return "customerEventEdit";
	}
	/**
	 * 功能描述:修改客服事件<br>
	 * @param custServiceEvent
	 * Create author:yixianjian <br>
	 * Create on:2008-12-16  <br>
	 */
	@SuppressWarnings("static-access")
	public String editCSEvent(@Read CustServiceEventVO custServiceEvent,
							  @Read(key="submitStatus")String status){
		custServiceEvent.setModifier(super.getUserId());
		CustServiceEventVO custServiceEventVO = eventManageService.editCustEvent(custServiceEvent);
		setAttribute(this.CACHE_KEY_EVENT, custServiceEventVO);

		initCustomerEventRegion();//初始化事件区域
		initCustomerEventNature();//初始化事件性质
		initEmergencyDegree();//初始化紧急程度
		initFrequentEvent();//初始化频发事件
		initSeriousDegree();//初始化严重程度
		initCustomerEventKind(custServiceEventVO.getEventNatureKeyword());//初始化事件类型

		initOptionStatus(this.OPTION_STATUS_EDIT); //设置操作为修改
		initSubmitStatus(true,status);
		return "customerEventEdit";
	}
	/**
	 * 功能描述:作废事件<br>
	 * @param custEventKeyword
	 * Create author:yixianjian <br>
	 * Create on:2008-12-16  <br>
	 */
	public void setAsideCustEvent(@Read CustServiceEventVO custServiceEvent){
		eventManageService.setAsideCustEvent(custServiceEvent);
	}

	/**
	 * 功能描述: 保存查询条件<br>
	 * @param custServiceEvent
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	public void setCustomerEventSearchCondition(@Read CustServiceEventSearchVO custServiceEventVO){
		setAttribute(SEARCH_CONDTION_KEY, custServiceEventVO,Scope.SESSION);
	}
	/**
	 * 功能描述:获取保存的查询条件<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	public CustServiceEventSearchVO getCustomerEventSearchCondtion(){
		CustServiceEventSearchVO vo = (CustServiceEventSearchVO)getAttribute(SEARCH_CONDTION_KEY);
		return vo == null ? new CustServiceEventSearchVO():vo;
	}
	/**
	 * 功能描述: 新建关联事件<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-24  <br>
	 */
	public void associateCSEvent(@Read(key="eventId")Long eventId,
									@Read(key="associatedIDs")String associatedIDs){

		eventManageService.associateCSEvent(eventId, processassociatedCSEventIDs(associatedIDs));
	}
	/**
	 * 功能描述: 解除事件关联<br>
	 * @param mastEventID
	 * @param associatedEventID
	 * Create author:yixianjian <br>
	 * Create on:2008-12-24  <br>
	 */
	public void riddanceCSEvent(@Read(key="eventId")Long mastEventID,
								@Read(key="associatedEventID")Long associatedEventID){
		eventManageService.riddanceCSEvent(mastEventID, associatedEventID);
	}

	/**
	 * 功能描述:归档客服事件<br>
	 * @param custServiceEventVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-22  <br>
	 */
	public void archivism(@Read CustServiceEventVO custServiceEventVO){
		eventManageService.archivism(custServiceEventVO);
	}
	/**
	 * 功能描述:事件完工<br>
	 * @param eventId
	 * Create author:yixianjian <br>
	 * Create on:2008-12-26  <br>
	 */
	public void manualTriggerComplete(@Read(key="eventId")Long eventId){
		eventManageService.manualTriggerComplete(eventId);
	}
	/**
	 * 功能描述: 事件性质、事件类型下拉联动<br>
	 * @param selectId
	 * @param targetId
	 * Create author:yixianjian <br>
	 * Create on:2008-12-15  <br>
	 * @throws Exception
	 */
	public void unitedSelect(@Read(key="selectId")Long eventNatureId,
							 @Read(key="targetId")String targetId) throws Exception{
		List<CustEventKindVO> custEventKindList = custEventKindService.queryCustEventKind(eventNatureId);
		String selectXML = XMLAbout.convertListToSelectXML(custEventKindList, "name", "keyword");
		renderXML(selectXML);
	}
	/**
	 * 功能描述:验收是否可以归档<br>
	 * @param csEventID
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public void validationAllowArchivism(@Read(key="csEventID") Long csEventID){
		eventManageService.isAllowArchivism(csEventID);
	}
	/**
	 * 功能描述:验证房号是否存在<br>
	 * @param houseNumber
	 * Create author:yixianjian <br>
	 * Create on:2009-1-13  <br>
	 */
	public void validationHouseNumber(@Read(key="houseNumber")String houseNumber){
		HouseDomain houseDomain = houseService.getHouseByNumber(houseNumber.trim());
		if(houseDomain == null) super.renderText("false");
		else super.renderText("true");
	}
	/**
	 * 功能描述:验证事件是否可被修改<br>
	 * @param eventId
	 * Create author:yixianjian <br>
	 * Create on:2009-1-14  <br>
	 */
	public void validationIsAllowEventEdit(@Read(key="eventId")Long eventId){
		eventManageService.isAllowEditCustEvent(eventId);
	}
	/**
	 * 功能描述:验证事件是否可被作废<br>
	 * @param eventId
	 * Create author:yixianjian <br>
	 * Create on:2009-1-14  <br>
	 */
	public void validationIsAllowSetAsideEvent(@Read(key="eventId")Long eventId){
		eventManageService.isAllowSetAsideEvent(eventId);
	}
	/**
	 * 功能描述:事件关联时,前台传一个ID字符串,用","分割,该方法就处理将字符串转换为List<br>
	 * @param associatedIDs
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-24  <br>
	 */
	private List<Long> processassociatedCSEventIDs(String associatedIDs){
		if(BlankUtil.isBlank(associatedIDs)) return null;

		List<Long> ids = new ArrayList<Long>();

		String[] ids_str = associatedIDs.split(",");
		for(int i = 0; i < ids_str.length; i++){
			ids.add(Long.valueOf(ids_str[i]));
		}
		return ids;
	}
	/**
	 * 功能描述:设置操作状态<br>
	 * @param option
	 * Create author:yixianjian <br>
	 * Create on:2008-12-16  <br>
	 */
	private void initOptionStatus(String option){
		if(OPTION_STATUS_ADD.equals(option))
			setAttribute(OPTION_STATUS, OPTION_STATUS_ADD);
		else if(OPTION_STATUS_EDIT.equals(option))
			setAttribute(OPTION_STATUS, OPTION_STATUS_EDIT);
	}
	/**
	 * 功能描述: 存储一个标识  看是否刷新页面<br>
	 * true   刷新 false不刷新
	 * @param isFlush
	 * Create author:yixianjian <br>
	 * Create on:2008-12-25  <br>
	 */
	private void initSubmitStatus(Boolean isFlush,String submitStatus){
		setAttribute("isFlush", isFlush);
		if(BlankUtil.isBlank(submitStatus)) return;
		setAttribute(submitStatus, Boolean.TRUE);
	}
	/**
	 * 功能描述:初始化客服事件区域<br>
	 * 查询相关事件区域并保存到request
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	private void initCustomerEventRegion(){
		List<EventRegionVO> eventRegions = eventRegionService.getEventRegionList();
		setAttribute("eventRegions", eventRegions);
	}
	/**
	 * 功能描述:初始化事件性质<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 */
	private void initCustomerEventNature(){
		List<CustEventNatureVO> custServEventNatureList = custEventNatureService.queryAllCustEventNature();
		setAttribute("eventNature", custServEventNatureList);
	}
	/**
	 * 功能描述:初始化事件类型<br>
	 * @param eventNatureId
	 * Create author:yixianjian <br>
	 * Create on:2008-12-25  <br>
	 */
	private void initCustomerEventKind(Long eventNatureId){
		List<CustEventKindVO> custEventKindList = custEventKindService.queryCustEventKind(eventNatureId);
		setAttribute("custEventKinds", custEventKindList);
	}
	/**
	 * 功能描述: 初始化频发事件  登记事件时记录事件内容时用<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-24  <br>
	 */
	private void initFrequentEvent(){
		List<FrequentEventVO> list = frequentEventService.queryAllFrequentEventWithValidity();
		setAttribute("FrequentEvent", list);
	}
	/**
	 * 功能描述:缓存紧急程度<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-25  <br>
	 */
	private void initEmergencyDegree(){
		List<EmergencyDegreeVO> emergencyDegrees = emergencyDegreeService.getEmergencyDegreeList();
		setAttribute("emergencyDegrees", emergencyDegrees);
	}
	/**
	 * 功能描述:初始化严重程度<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-25  <br>
	 */
	private void initSeriousDegree(){
		List<SeriousDegreeVO> seriousDegrees = seriousDegreeService.getSeriousDegreeList();
		setAttribute("seriousDegrees", seriousDegrees);
	}


	public void setEventManageService(IEventManageService eventManageService) {
		this.eventManageService = eventManageService;
	}

	public void setEventRegionService(IEventRegionService eventRegionService) {
		this.eventRegionService = eventRegionService;
	}

	public void setCustEventNatureService(
			ICustEventNatureService custEventNatureService) {
		this.custEventNatureService = custEventNatureService;
	}

	public void setCustEventKindService(ICustEventKindService custEventKindService) {
		this.custEventKindService = custEventKindService;
	}

	public void setFrequentEventService(IFrequentEventService frequentEventService) {
		this.frequentEventService = frequentEventService;
	}

	public void setEmergencyDegreeService(
			IEmergencyDegreeService emergencyDegreeService) {
		this.emergencyDegreeService = emergencyDegreeService;
	}

	public void setSeriousDegreeService(ISeriousDegreeService seriousDegreeService) {
		this.seriousDegreeService = seriousDegreeService;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

}
